<template>
  <div class="personal">
    <router-link :to="'/editPersonal/' + $route.params.id">
      <div class="profile">
        <img :src="userInfo.head_img" alt="" />
        <div class="profile-center">
          <div class="name">
            <span
              :class="{
                iconfont: true,
                iconxingbienan: userInfo.gender == 1,
                iconxingbienv: userInfo.gender == 0,
              }"
            ></span>
            {{ userInfo.nickname }}
          </div>
          <div class="time">{{ userInfo.create_date | dateFormat("-") }}</div>
        </div>
        <span class="iconfont iconjiantou1"></span>
      </div>
    </router-link>
    <hmcell
      title="我的关注"
      desc="关注的用户"
      @click="$router.push({ name: 'myfollows' })"
    ></hmcell>
    <hmcell title="我的跟帖" desc="跟帖/回复"></hmcell>
    <hmcell
      title="我的收藏"
      desc="文章/视频"
      @click="$router.push({ name: 'mystars' })"
    ></hmcell>
    <hmcell title="设置"></hmcell>
    <hmbutton type="danger" @click="exit">退出</hmbutton>
  </div>
</template>

<script>
/* 不能在模板结构中直接使用引入的axios模板对象进行拼接，因为模板中能够使用的数据是当前组件实例的成员(也就是说模板上不可以直接
使用console.log或者import导入的axios，需要在data中定义成员)更推荐数据改造方式*/
import hmcell from "@/components/hmcell.vue";
import hmbutton from "@/components/hmbutton.vue";
import { getUserById } from "@/apis/user.js";
import { Toast } from "vant";
import axios from "@/utils/myaxios.js";
import { dateFormat } from "@/utils/myfilters.js";
export default {
  components: {
    hmcell,
    hmbutton,
  },
  async mounted() {
    // console.log(this.$route.params.id);
    try {
      let res = await getUserById(this.$route.params.id);
      // console.log(res);
      if (res.status == 200) {
        // Toast.success(res.data.message);
        // 先处理数据
        res.data.data.head_img =
          axios.defaults.baseURL + res.data.data.head_img;
        this.userInfo = res.data.data;
        // this.head_img = "http://127.0.0.1:3000" + userInfo.head_img;
      } else {
        Toast.fail(res.data.message);
        this.$router.push({ name: "login" });
      }
    } catch {
      Toast.fail("获取用户信息异常,尝试重新登陆！");
    }
  },
  data() {
    return {
      userInfo: {},
      // axios
    };
  },
  methods: {
    exit() {
      console.log(21);
      localStorage.removeItem("hmtoutiao_token");
      localStorage.removeItem("hmtoutiao_pwd");
      localStorage.removeItem("user_id");
      this.$router.push({ name: "index" });
    },
  },
  filters: {
    dateFormat,
  },
};
</script>

<style lang='less' scoped>
.personal {
  width: 100vw;
  height: 100vh;
  background-color: #eee;
}
a {
  text-decoration: none;
  color: #666;
}
.profile {
  display: flex;
  padding: 20px 10px;
  justify-content: space-between;
  align-items: center;
  border-bottom: 5px solid #ddd;
  img {
    width: 70 * 100vw/360;
    height: 70 * 100vw/360;
    border-radius: 50%;
  }
  .profile-center {
    flex: 1;
    padding: 0 10px;
    display: flex;
    flex-direction: column;
    justify-items: center;
    font-size: 14px;
    .name {
      margin-bottom: 5px;
      span {
        color: #75b9eb;
      }
    }
  }
}
.btn {
  margin-top: 20px;
}
</style>
